var router = require('libs/router').router;
var pages = require('libs/pages');
var ItemMixin = require('mixins/item');
var libs = require('libs/libs');
var api = libs.api;
var store = require('mixins/store');

var _getLogin = SA.get('USER').login;       //获取login值
var Comments_id  = '';     //上个页面传过来的id
var Comments_com = '';      //上个页面传过来的状态


var bindEvent = function(){
    //此处各种dom 操作
    libs.inject()
        .js(['/js/t/jq/ui.js', 'uiLib'], evaluate)
    function evaluate(){
        $(".star_on i").click(function (){
            var l=$(this).index();
            $(".star_on i").removeClass("starCheck");
            for(var i=0;i<=l;i++){
                $(".star_on i").eq(i).addClass("starCheck");
            }
            SA.set('Star',{data: l})
        })
    }
    //已评论星星的显示情况
    function showStart(){
        $('.star_off i').each(function(){
            var getStartdata = $(this).parent('.star_off').attr('data-val') -1;   //获取的值减于1是为了遍历是从0开始的
            for(var i=0;i<=getStartdata;i++){
                $(".star_off i").eq(i).addClass("starCheck");
            }
        })
    }
    showStart()
    $('#Comments_btn a').click(function(){
        var sources = SA.get('Star').data;
        var Comments_post = {
            login: SA.get('USER').login,
            orderId: Comments_id,
            score: sources + 1,
            method: 'get'
        }
        api.req('/Ordercomments', Comments_post, function(data){
            if(data.result){
                SA.setter('Updatestatus',{comment:{backstatus:'COMPLETEEVALUATE',score: sources + 1}})
                showStart()
            //     ui.alertBox({
            //         cons: [{text:data.result,name:"bigCon"}],
            //         btns: [
            //             {
            //                 text: '确定',
            //                 close: true,
            //                 callback: function () {
            //                     SA.setter('Updatestatus',{data: 'COMPLETEEVALUATE'})
            //                     // router.goback()
            //                 //    router('water/item', {backstatus: 'COMPLETEEVALUATE'})
            //                 }
            //             }
            //         ],
            //         cols:function (){
            //             SA.setter('Updatestatus',{data: 'COMPLETEEVALUATE'})
            //             // router.goback()
            //             // router('water/item', {backstatus: 'COMPLETEEVALUATE'})
            //         }
            //     })
            // }else{
            //     ui.alertBox({
            //         cons: [{text:data.message,name:"bigCon"}],
            //         btns: [
            //         ]
            //     })
            }
        })
    })

};

//演示模块
var Comments = React.createClass({
    mixins: [ItemMixin, store('Updatestatus')],
    componentWillMount: function(){
        if(this.props.data){
            this.setState({
                comment: this.props.data,
                score: 1
            })
        }
    },
    willMount: function(){
        if(this.state.comment.backstatus == 'TOEVALUATE'){
            return(
                <div className="evaluateWarp flex flex-v">
                    <div className="flex-1 f-oTouch">
                        <p>感谢您使用大白管家服务<br/>请对本次服务进行评价</p>
                        <div className="starBox1 star_on">
                            <i></i>
                            <i></i>
                            <i></i>
                            <i></i>
                            <i></i>
                        </div>
                        <div className="evaluateBtn f_submit u-mt-15" id="Comments_btn">
                            <a href='javascript:;' className="f_btn">提交</a>
                        </div>
                    </div>
                </div>
            )
        }
        else{
            return(
                <div className="evaluateWarp flex flex-v">
                    <p>感谢您的评价!</p>
                    <div data-val={this.state.comment.score} className="starBox1 star_off" >
                        <i></i>
                        <i></i>
                        <i></i>
                        <i></i>
                        <i></i>
                    </div>
                </div>
            )
        }
    },
    render:function(){
        var comment = this.willMount()
        return (
            <div>{comment}</div>
        )
    }
});

function start(name){
    return pages.new({
        boot: function(){
        },
        trigger:function(self, data){
            this.libs.changeTitle('评价');    //更改当前页面标题
            if(data && data.backid && data.backstatus){
                Comments_id = data.backid,
                Comments_com = data.backstatus
            }
            if(Comments_com == 'COMPLETEEVALUATE'){
                var CommentsOff_post ={
                    login: SA.get('USER').login,
                    orderId: Comments_id,
                    method: 'get'
                }
                api.req('/Ordercomments_off', CommentsOff_post, function(data){
                    self.main(self, data)
                })
            }else if(Comments_com == 'TOEVALUATE'){
                self.main(self, data)
            }

        },
        main: function(self, data){
            this.render(
                <Comments itemDefaultMethod={bindEvent} data={data} />,
                document.getElementById(name)
            )
        },
        end: function(){
            React.unmountComponentAtNode(document.getElementById(name))
        }
    })
}

module.exports = start;
